package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.SizeUtils
import com.lv.common.imp.OnPositionTListener
import com.lv.common.utils.showToast
import com.lv.service.R
import com.lv.service.adapter.CardCategoryImageBottomAdapter
import com.lv.service.bean.TelSecondCategory
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class CategoryImageSelectedPopupView(
    private val mContext: Context,
    private var mIsOpen: Boolean = false,
    private val mAllCategory: List<TelSecondCategory>? = null,
    private val selectedCategorys: List<TelSecondCategory>? = null,
    private val showMore: Boolean = false,
    private var onPopSureListener: OnPositionTListener<List<TelSecondCategory>>?
) :
    BottomPopupView(mContext) {
    private val MAXA_SELECTED = 5
    private var maxSelected = 1
    private var mPosition = 0
    private val name = ""
    private var rvContent: RecyclerView? = null
    private var viewTop: View? = null
    private var viewBottom: View? = null
    private var vSumbit: View? = null
    private var tvSubmit: TextView? = null
    private var llMoreCategory: LinearLayout? = null
    private val mCategorys = mutableListOf<TelSecondCategory>()

    private val mSelectedCategorys = mutableListOf<TelSecondCategory>()
    private var mOnPopSwitchListener: OnPositionTListener<Int>? = null

    private var mAdapter: CardCategoryImageBottomAdapter? = null
    override fun getImplLayoutId(): Int {
        return R.layout.category_image_bottom_popup
    }

    override fun onCreate() {
        super.onCreate()
        rvContent = findViewById<RecyclerView>(R.id.rv_content)
        viewTop = findViewById<View>(R.id.view_top)
        viewBottom = findViewById<View>(R.id.view_bottom)
        vSumbit = findViewById<View>(R.id.v_sumbit)
        tvSubmit = findViewById<TextView>(R.id.tv_submit)
        llMoreCategory = findViewById<LinearLayout>(R.id.ll_more_category)
        findViewById<ImageView>(R.id.iv_close).setOnClickListener { dismiss() }
        llMoreCategory?.setOnClickListener {
            mOnPopSwitchListener?.onClick(0, 0)
        }
        llMoreCategory?.visibility = if (showMore) View.VISIBLE else View.GONE
        initView()
        initEvnet()
    }


    override fun onShow() {
        super.onShow()
        initData()

    }

    private fun initView() {
        initAdapter()
        initData()

        initEnable()
    }

    private fun initAdapter() {
        val gridLayoutManager = GridLayoutManager(context, 5)
        rvContent?.layoutManager = gridLayoutManager
        mAdapter = CardCategoryImageBottomAdapter(mCategorys)
        rvContent?.adapter = mAdapter
    }

    private fun initData() {
        maxSelected = if (mIsOpen) MAXA_SELECTED else 1
        mAllCategory?.let {
            mCategorys.clear()
            mCategorys.addAll(mAllCategory)
        }
        mSelectedCategorys.clear()
        selectedCategorys?.let {
            mSelectedCategorys.addAll(selectedCategorys)
        }
        for (i in 0 until mCategorys.size) {
            val id = mCategorys[i].fcaid
            val secaid = mCategorys[i].secaid
            mCategorys[i].isSelcted = false
            for (j in 0 until mSelectedCategorys.size) {
                val id1 = mSelectedCategorys[j].fcaid
                val secaid1 = mSelectedCategorys[j].secaid
                if (id == id1 && secaid1 == secaid) {
                    mPosition = i
                    mCategorys[i].isSelcted = true
                    break
                }
            }
        }
        mAdapter?.notifyDataSetChanged()
    }

    private fun initEnable() {
//        val mSelectedCategorys =
//            mCategorys.filter { item -> (!item.isItemHeader) && item.isSelcted }
        if (CollectionUtils.isNotEmpty(mSelectedCategorys)) {
            tvSubmit?.alpha = 1.0f
            vSumbit?.isClickable = true
        } else {
            tvSubmit?.alpha = 0.6f
            vSumbit?.isClickable = false
        }
    }

    private fun initEvnet() {

        viewTop?.setOnClickListener {
            dismiss()
        }
        mAdapter?.onViewItemClickListener = OnPositionTListener { position, data ->
            val selected = !data.isSelcted
            if (selected) {
                if (maxSelected == 1) {
                    mSelectedCategorys.clear()
                    if (mPosition == -1) {
                        mCategorys[position].isSelcted = selected
                        mAdapter?.notifyItemChanged(position, R.id.iv_image_tag)
                        mSelectedCategorys.add(data)
                    } else {
                        mCategorys[mPosition].isSelcted = !selected
                        mAdapter?.notifyItemChanged(mPosition, R.id.iv_image_tag)
                        mSelectedCategorys.remove(mCategorys[mPosition])
                        mSelectedCategorys.add(data)
                        mCategorys[position].isSelcted = selected
                        mAdapter?.notifyItemChanged(position, R.id.iv_image_tag)
                    }
                    mPosition = position
                } else {
                    if (mSelectedCategorys.size < maxSelected) {
                        mCategorys[position].isSelcted = selected
                        mAdapter?.notifyItemChanged(position, R.id.iv_image_tag)
                        mSelectedCategorys.add(data)
                        mPosition = position
                    } else {
                        showToast("最多只能选择${maxSelected}个分类")
                    }
                }

            } else {
                mCategorys[position].isSelcted = selected
                mAdapter?.notifyItemChanged(position, R.id.iv_image_tag)
                for (k in 0 until mSelectedCategorys.size) {
                    val telSecondCategory = mSelectedCategorys[k]
                    if (data.fcaid == telSecondCategory.fcaid && data.secaid == telSecondCategory.secaid) {
                        mSelectedCategorys.removeAt(k)
                        break
                    }
                }
                if (mSelectedCategorys.size == 0) {
                    mPosition = -1
                }
            }
            initEnable()
        }
        vSumbit?.setOnClickListener {
//            onPopSureListener?.onClick(0, mSelectedCategorys)
//            mCategorys.forEachIndexed { index, telSecondCategory ->
//
//            }
//            for (i in 0 until mCategorys.size) {
//                val id = mCategorys[i].id
//                for (j in 0 until mSelectedCategorys.size) {
//                    val id1 = mSelectedCategorys[j].id
//                    mCategorys[i].isSelcted = id == id1
//                }
//            }
            onPopSureListener?.onClick(0, mSelectedCategorys)
            dismiss()
        }

    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }


    fun setOnSwitchListener(onPopSwitchListener: OnPositionTListener<Int>?) {
        this.mOnPopSwitchListener = onPopSwitchListener
    }

    fun setOpen(isOpen: Boolean) {
        mIsOpen = isOpen
        maxSelected = if (mIsOpen) MAXA_SELECTED else 1
        if (!mIsOpen) {
            for (i in 0 until mCategorys.size) {
                val TelSecondCategory = mCategorys[i]
                if ((!TelSecondCategory.isItemHeader) && TelSecondCategory.isSelcted) {
                    mCategorys[i].isSelcted = false
                    mAdapter?.notifyItemChanged(i, R.id.iv_image_tag)
                }
            }
//            val mSelectedCategorys =
//                mCategorys.filter { item -> (!item.isItemHeader) && item.isSelcted }
            if (mSelectedCategorys.isNotEmpty()) {
                val TelSecondCategory1 = mSelectedCategorys[0]
                val fcaid = TelSecondCategory1.fcaid
                val secaid1 = TelSecondCategory1.secaid
                mPosition = -1
                for (i in 0 until mCategorys.size) {
                    val id = mCategorys[i].fcaid
                    val secaid = mCategorys[i].secaid
//                    if (TelSecondCategory1.id == mCategorys[i].id) {
                    if (fcaid == id && secaid1 == secaid) {
                        mPosition = i
                        mCategorys[mPosition].isSelcted = true
                        mAdapter?.notifyItemChanged(mPosition, R.id.iv_image_tag)
                    }
                }

                mSelectedCategorys.clear()
                mSelectedCategorys.add(TelSecondCategory1)
            }
        }
        onPopSureListener?.onClick(0, mSelectedCategorys)

//        mSelectedCategorys.clear()
//        if (mPosition != -1) {
//            mSelectedCategorys.add(mCategorys[mPosition])
//        }
        initEnable()
    }
}